package com.ffcs.crmd.tsp.server.jdbc.service.impl;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.ffcs.crmd.tsp.common.exception.TspException;
import com.ffcs.crmd.tsp.server.jdbc.entity.Slice;
import com.ffcs.crmd.tsp.server.jdbc.entity.SliceGroup;
import com.ffcs.crmd.tsp.server.jdbc.repository.SliceGroupRepository;
import com.ffcs.crmd.tsp.server.jdbc.service.ISliceGroupService;
import com.ffcs.crmd.tsp.server.jdbc.service.ISliceService;

/**
 * 
 * 功能说明:任务服务
 *
 * @author ZHONGFUHUA
 * 
 * @since 1.0.0-SNAPSHOT
 *
 */
public class SliceGroupServiceImpl implements ISliceGroupService {
    
    private SliceGroupRepository sliceGroupRepository = new SliceGroupRepository();;
    
    private ISliceService        sliceService;
    
    public SliceGroupServiceImpl() {
        
    }
    
    @Override
    public SliceGroup get(Connection conn, SliceGroup sliceGroup) throws TspException {
        List<Object> params = new ArrayList<Object>();
        params.add(sliceGroup.getName());
        params.add(sliceGroup.getJobId());
        params.add(sliceGroup.getId());
        return sliceGroupRepository.query(GET_SLICE_GROUP, params, conn);
    }
    
    @Override
    public int querySize(Connection conn, SliceGroup sliceGroup) throws TspException {
        List<Object> params = new ArrayList<Object>();
        String sql = rtp(SELECT_SLICE_GROUP, sliceGroup, params, true);
        return sliceGroupRepository.queryRows(sql, params, conn);
    }
    
    @Override
    public List<SliceGroup> query(Connection conn, SliceGroup sliceGroup) throws TspException {
        List<Object> params = new ArrayList<Object>();
        String sql = rtp(SELECT_SLICE_GROUP, sliceGroup, params, false);
        return sliceGroupRepository.queryList(sql, params, conn);
    }
    
    @Override
    public List<SliceGroup> queryPage(Connection conn, SliceGroup sliceGroup) throws TspException {
        List<Object> params = new ArrayList<Object>();
        String sql = rtpPage(SELECT_SLICE_GROUP, sliceGroup, params);
        return sliceGroupRepository.queryList(sql, params, conn);
    }
    
    @Override
    public boolean add(Connection conn, SliceGroup sliceGroup) throws TspException {
        List<Object> params = new ArrayList<Object>();
        params.add(TspSeqServiceImpl.getSeq());
        params.add(sliceGroup.getName());
        params.add(sliceGroup.getJobId());
        params.add(sliceGroup.getClusterName());
        return sliceGroupRepository.execute(INSERT_SLICE_GROUP, params, conn);
    }
    
    @Override
    public boolean delete(Connection conn, SliceGroup sliceGroup) throws TspException {
        List<Object> params = new ArrayList<Object>();
        String sql = rtpDelete(DELETE_SLICE_GROUP, sliceGroup, params);
        List<SliceGroup> sliceGroupList = new ArrayList<SliceGroup>(); 
        
        SliceGroup qsliceGroup = this.get(conn, sliceGroup);
        if(qsliceGroup != null){
            sliceGroupList.add(qsliceGroup);
        }else{
            sliceGroupList.addAll(this.query(conn, sliceGroup));
        }
        
        for (SliceGroup rsliceGroup : sliceGroupList) {
            Slice slice = new Slice();
            slice.setSliceGroupId(rsliceGroup.getId());
            sliceService.delete(conn, slice);
        }
        return sliceGroupRepository.execute(sql, params, conn);
    }
    
    @Override
    public boolean update(Connection conn, SliceGroup sliceGroup) throws TspException {
        List<Object> params = new ArrayList<Object>();
        params.add(sliceGroup.getName());
        params.add(sliceGroup.getJobId());
        params.add(sliceGroup.getId());
        return sliceGroupRepository.execute(UPDATE_SLICE_GROUP, params, conn);
    }
    
    
    /**
     * 
     * 功能说明:
     * 
     * @param sql
     * @param sliceGroup
     * @param params
     * @return
     */
    private String rtp(String sql, SliceGroup sliceGroup, final List<Object> params, boolean isLike) {
        if (sliceGroup.getJobId() != null) {
            sql += " AND TSG." + COL_SLICE_GROUP_JOB_ID + " = ?";
            params.add(sliceGroup.getJobId());
        }
        
        if (StringUtils.isNoneBlank(sliceGroup.getName())) {
            if (isLike) {
                sql += " AND TSG." + COL_NAME + " LIKE ?";
                params.add("%" + sliceGroup.getName() + "%");
            }else{
                sql += " AND TSG." + COL_NAME + " = ?";
                params.add(sliceGroup.getName());
            }
        }
        
        if (StringUtils.isNoneBlank(sliceGroup.getClusterName())) {
            sql += " AND TSG." + COL_CLUSTER_NAME + " = ?";
            params.add(sliceGroup.getClusterName());
        }
        
        return sql;
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @param sql
     * @param sliceGroup
     * @param params
     * @return
     */
    private String rtpPage(String sql, SliceGroup sliceGroup, final List<Object> params) {
        String rsql = rtp(sql, sliceGroup, params, true);
        rsql += " LIMIT ?,?";
        params.add(sliceGroup.getPageNumber());
        params.add(sliceGroup.getPageSize());
        return rsql;
    }
    
    
    /**
     * 
     * 功能说明:
     * 
     * @param sql
     * @param sliceGroup
     * @param params
     * @return
     * @throws TspException 
     */
    private String rtpDelete(String sql, SliceGroup sliceGroup, final List<Object> params) throws TspException {
        if (sliceGroup.getJobId() == null && sliceGroup.getId() == null) {
            throw new TspException("delete job ['id' or 'jobId'] can't be null at the same time！");
        }
        
        if (sliceGroup.getId() != null) {
            sql += " AND " + COL_ID + " = ?";
            params.add(sliceGroup.getId());
        }
        
        if (sliceGroup.getJobId() != null) {
            sql += " AND " + COL_SLICE_GROUP_JOB_ID + " = ?";
            params.add(sliceGroup.getJobId());
        }
        
        return sql;
    }
    
    
    /**
     * 功能说明: 设置sliceService
     *
     * @param sliceService sliceService 
     */
    @Override
    public void setSliceService(ISliceService sliceService) {
        this.sliceService = sliceService;
    }
    
}
